var util = require('./util');
var md5 = require('./md5');
var Config = require('../Config');
var HTTP_KEY = 'ocmcom_pt';
var getLoginInfo = function (callback) {
  wx.getStorage({
    key: 'teacherInfo',
    success: function (res) {
      callback(res.data)
    },
    fail() {
      wx.showToast({
        title: '获取登录信息失败',
        icon: 'none',
        duration: 2000
      })
    }
  })
}
var request = function (url, data, successFun, errorFun, method) {
  var times = Math.floor(new Date().getTime() / 1000);
  var option = {
    time: times,
    sign: md5.hex_md5(HTTP_KEY + '_' + times)
  };
  wx.request({
    url: Config.HTTP_BASE_URL + url,
    data: util.extend(true, option, data),
    method: method,
    success(res) {
      if (!res.data.flag) {
        console.error(res.data.msg);
      }
      successFun(res.data);
    },
    fail() {
      if (errorFun) {
        errorFun();
      } else {
        wx.hideLoading();
      }
    }
  })
}
module.exports = {
  get: function(url, data, successFun, errorFun) {
    request(url, data, successFun, errorFun, 'GET');
  },
  post: function (url, data, successFun, errorFun) {
    request(url, data, successFun, errorFun, 'POST');
  },
  getLoginInfo: getLoginInfo
}
